package com.qf.usefull.dict.service.impl;

import com.qf.usefull.dict.entity.Word;
import com.qf.usefull.dict.service.MyDictionary;

import java.util.Arrays;

public class MyDictImpl01 extends AbstractMyDictImpl implements MyDictionary {

    public MyDictImpl01() {
    }
    /**
     * 构造方法，可以提供底层数组的长度
     * @param capacity
     */
    public MyDictImpl01(int capacity) {
        super(capacity);
    }

    @Override
    public void addWord(String english, String chinese) {
        Word word = new Word(english, chinese);
//        int count = getCount();
        Word[] arr = getArr();
        // 判断底层数组的长度是否足够
        if (getCount() == arr.length){
            arr = Arrays.copyOf(arr,arr.length*2);
        }
        // word对象存放到哪里呢？
        int count = getCount();
        arr[count++] = word;
        setCount(count);
    }

    @Override
    public String findWord(String english) {
        int count = getCount();
        Word[] arr = getArr();
        for (int i = 0; i < count; i++) {
                if (arr[i].getEnglish().equals(english))
                    return arr[i].getChinese();
        }

        return null;
    }
}
